Skip to content

Conversation

@Chengxuan
Copy link
Contributor

@Chengxuan Chengxuan commented Jan 13, 2026

This PR adds support for PostgreSQL's DISTINCT ON clause to the firefly-common library, enabling efficient querying of distinct rows based on specific field combinations. This feature is PostgreSQL-specific and provides a more performant alternative to using GROUP BY or subqueries for deduplication scenarios.

Postgres ONLY

The feature explicitly validates that the database provider is PostgreSQL before applying DISTINCT ON, returning a clear error message for unsupported providers.

Usage Example

filter := queryFactory.NewFilter(ctx).
    DistinctOn("namespace", "type").
    Sort("created").
    And()

results, _, err := collection.GetMany(ctx, filter)

This generates SQL like:

SELECT DISTINCT ON (namespace, type) * 
FROM table 
ORDER BY namespace, type, created

Signed-off-by: Chengxuan Xing <chengxuan.xing@kaleido.io>
@Chengxuan Chengxuan requested a review from a team as a code owner January 13, 2026 12:01
@peterbroadhurst
Copy link
Contributor

The Firefly filtering system is very deliberately not-aligned to SQL syntax currently.

There is support in the library for code to use low-level SQL via extensions.

In this case, this seems to be trying to describe to end users of all APIs built on FF-common a new filtering syntax.

So to tackle the decisions around spelling and exposure problem I think we need, a description of the kind of use case that would require this. Maybe a reasoning for why you chose to push it all the way to the external API, and furthermore to make it specific to PostgreSQL (it's not a PSQL specific construct - I'm sure MongoDB and others have similar constructs).

Then if we decide it makes sense to expose:

  1. To all users on all REST APIs with some naming - that would mean updating this PR to cover all the cases
    • The REST filters
    • The API builders (Typescript and Go)
  2. Something very specific to a particular use case - here the question is why the existing extensions didn't help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants